<?php
namespace lib;

class Autoload{

    /**
     * 命名空间列表
     * @var array
     */
    protected static $namespace = [
        'lib'       => LIB_PATH,
        'app'       => APP_PATH,
        'core'      => CORE_PATH,
        'extend'    => EXTEND_PATH,
    ];

    /**
     * 注册自动加载
     * @return bool
     */
    public static function listen(){
        return spl_autoload_register(function($class){
            list($type, $className) = explode('\\', $class, 2);
            if(!isset(self::$namespace[$type])){
                throw new \Exception("调用未定义模块[{$type}]");
            }
            $path = self::$namespace[$type];
            $filename = str_replace('\\', '/', "{$path}{$className}" . EXT);
            if(file_exists($filename)){
                require_once $filename;
            }else{
                throw new \Exception("调用未定义类[{$class}]");
            }
        });
    }

    /**
     * 注册命名空间
     * @param array $options
     * @return array
     */
    public static function register(array $options){
        return self::$namespace = array_merge(self::$namespace, $options);
    }

}